package com.rabbitmqdemo.eureka.controller;

import com.rabbitmqdemo.eureka.service.impl.ProducerImpl;
import com.rabbitmqdemo.eureka.util.GetTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
public class MainController {

    @Autowired
    ProducerImpl producer;

    /**
     * 向交换器发送消息，并返回json数据 （spring-cloud-starter-netflix-eureka-server 引入 jackson-dataformat-xml 导致默认返回XML）
     * MediaType.APPLICATION_JSON_UTF8_VALUE 等同 {"application/json;charset=UTF-8"}
     */
    @RequestMapping(value = "/produce", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map<String, String> sendMsgToExchange() {
        String now = GetTime.getFormatNow();
        producer.sendMsg(now + " " + "发送的消息");
        Map<String, String> map = new HashMap<>();
        map.put("result", "success");
        map.put("sendTime", now);
        return map;
    }

}
